package com.designCreator.model.business
{
	import com.designCreator.model.DesignCreatorProxy;
	import com.designCreator.model.vo.DesignCreatorRequest;
	
	import flash.events.ErrorEvent;
	import flash.events.Event;
	import flash.events.SecurityErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	
	import mx.collections.XMLListCollection;
	import mx.controls.Alert;
	import mx.messaging.config.ServerConfig;
	import mx.rpc.AsyncToken;
	import mx.rpc.IResponder;
	import mx.rpc.Responder;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.http.HTTPService;
	import mx.rpc.remoting.RemoteObject;
	
	public class DesignCreatorDelegate implements iServices
	{
		private var _service : HTTPService;
		
		private var _token : AsyncToken;
		
		
		public function DesignCreatorDelegate()
		{
			_service = new HTTPService();
			_service.resultFormat = "e4x";
		}
		
		
		/**
		 * Load solid farics design 
		 * */
		public function loadSolidFabricsDesign(requestVO:DesignCreatorRequest):
			AsyncToken
		{
			_service.url = requestVO.solidFabricDesignsXMLPath;
			var localResponder : Responder = 
				new Responder( onLoadSolidFabrics, onErrorLoadSolidFabrics);
			var token : AsyncToken = _service.send();
			token.addResponder( localResponder );
			_token = new AsyncToken( token.message );
			return _token;
		}
		
		private function onLoadSolidFabrics(event:ResultEvent):void
		{
			Alert.show(''+event.result);
		}
		private function onErrorLoadSolidFabrics(event:FaultEvent):void
		{
			Alert.show(''+event.fault);
		}
	
		/**
		 * Load white farics design 
		 * */
		public function loadWhiteFabricsDesign(requestVO:DesignCreatorRequest):
			AsyncToken
		{
			_service.url = requestVO.whiteFabricDesignsXMLPath;
			var localResponder : Responder = 
				new Responder( onLoadWhiteFabrics, onErrorLoadWhiteFabrics );
			 
			var token : AsyncToken = _service.send();
			token.addResponder( localResponder );
			_token = new AsyncToken( token.message );
			return _token;
		}
		
		private function onLoadWhiteFabrics(event:ResultEvent):void
		{
			Alert.show(''+event.result);
		}
		private function onErrorLoadWhiteFabrics(event:FaultEvent):void
		{
			Alert.show(''+event.fault);
		}
		
		
			
		/*function loadL1Configuration(requestVO:DesignCreatorRequest):void
		{
			
			_service.url = url;
			var localResponder : Responder = new Responder( feedLoaded, loadFailed );
			var token : AsyncToken = _service.send();
			token.addResponder( localResponder );
			_token = new AsyncToken( token.message );
			return _token;
		}
		*/
		
		
		/*public function send( url : String ) : AsyncToken
		{
			_service.url = url;
			var localResponder : Responder = new Responder( feedLoaded, loadFailed );
			var token : AsyncToken = _service.send();
			token.addResponder( localResponder );
			_token = new AsyncToken( token.message );
			return _token;
		}
		
		private function feedLoaded( event : ResultEvent ) : void
		{
			var itunes : Namespace = new Namespace( "http://www.itunes.com/dtds/podcast-1.0.dtd" );
			var feed : Feed = new Feed;
			var channel : XML = ( event.result as XML ).channel[ 0 ];
			feed.title = channel.title;
			feed.url = channel.link;
			feed.description = channel.itunes::summary;
			if ( !feed.description )
			{
				feed.description = channel.description;
			}
			feed.image = channel.itunes::image.@href;
			
			feed.podcasts = new Array();
			for each ( var item : XML in channel.item )
			{
				var podcast : Podcast = new Podcast();
				podcast.title = item.title;
				podcast.description = item.itunes::summary;
				if ( !podcast.description )
				{
					podcast.description = item.description;
				}
				podcast.url = item.enclosure.@url;
				podcast.format = item.enclosure.@type;
				podcast.length = item.enclosure.@length;
				podcast.pubDate = new Date( Date.parse( item.pubDate.toString() ) );
				feed.podcasts.push( podcast );
			}
			
			feed.loaded = true;
			var resultEvent : ResultEvent = new ResultEvent( ResultEvent.RESULT, false, true, feed, _token );
			for each ( var responder : IResponder in _token.responders )
			{
				responder.result( resultEvent );
			}
		}*/
		
		private function faultHandler( event : FaultEvent ) : void
		{
			for each ( var responder : IResponder in _token.responders )
			{
				responder.fault( event );
			}
		}
	}
}